//
//  XGAnimation.m
//  YiCuiMiaoZhang
//
//  Created by Hosel on 2017/2/22.
//  Copyright © 2018 益萃网络科技（中国）有限公司. All rights reserved.
//

#import "BZAnimation.h"
#import <AudioToolbox/AudioToolbox.h>

// 屏幕宽度
#define Animation_SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
// 屏幕高度
#define Animation_SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)

@interface BZAnimation () <CAAnimationDelegate>

@property (nonatomic, weak) UIView *view;

@end

@implementation BZAnimation


- (void)logAnimation:(UIView *)view {
    
    self.view = view;
    
    UIView *containerView         = [[UIView alloc] initWithFrame:view.bounds];
    containerView.backgroundColor = [UIColor clearColor];
    // 对角线的一半作为半径
    CGFloat radius = sqrtf(containerView.frame.size.height * containerView.frame.size.height +
                           containerView.frame.size.width * containerView.frame.size.width) /
    2;
    
    UIBezierPath *startCycle = [UIBezierPath bezierPathWithArcCenter:containerView.center
                                                              radius:radius
                                                          startAngle:0
                                                            endAngle:M_PI * 2
                                                           clockwise:YES];
    UIBezierPath *endCycle = [UIBezierPath
                              bezierPathWithOvalInRect:CGRectMake(containerView.frame.size.width / 2 - 20,
                                                                  containerView.frame.size.height / 2 - 20, 40, 40)];
    
    //创建CAShapeLayer进行遮盖
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.fillColor     = [UIColor greenColor].CGColor;
    maskLayer.path          = endCycle.CGPath;
    view.layer.mask         = maskLayer;
    
    //创建路径动画
    CABasicAnimation *maskLayerAnimation = [CABasicAnimation animationWithKeyPath:@"path"];
    maskLayerAnimation.fromValue         = (__bridge id)(startCycle.CGPath);
    maskLayerAnimation.toValue           = (__bridge id)((endCycle.CGPath));
    maskLayerAnimation.duration          = 0.5; //[self transitionDuration:transitionContext];
    maskLayerAnimation.delegate          = self;
    maskLayerAnimation.timingFunction =
    [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    
    //    maskLayerAnimation.removedOnCompletion = NO;
    //    maskLayerAnimation.fillMode = kCAFillModeForwards;
    //    [maskLayerAnimation setValue:transitionContext forKey:@"transitionContext"];
    [maskLayer addAnimation:maskLayerAnimation forKey:@"path"];
}

- (void) loginFinishedAnimation:(UIView *) view AndSuccess:(BOOL) isSuccess {
    
    if (isSuccess) {
//        [view.layer removeAllAnimations];
//        [view.layer.mask removeFromSuperlayer];
//
//
//        UIView *containerView         = [[UIView alloc] initWithFrame:view.bounds];
//
//
//        UIBezierPath *startCycle = [UIBezierPath
//        bezierPathWithOvalInRect:CGRectMake(containerView.frame.size.width / 2 - 20,
//                                            containerView.frame.size.height / 2 - 20, 40, 40)];
//        UIBezierPath *endCycle = [UIBezierPath
//                                  bezierPathWithOvalInRect:CGRectMake(view.superview.center.x,
//                                                                      view.superview.center.y, Animation_SCREEN_WIDTH+100, Animation_SCREEN_HEIGHT+100)];
//
//        CAShapeLayer *maskLayer = [CAShapeLayer layer];
//        maskLayer.fillColor     = [UIColor greenColor].CGColor;
//        maskLayer.path          = startCycle.CGPath;
//        view.layer.mask         = maskLayer;
//
//        CGRect frame = view.frame;
//        frame.size.width = Animation_SCREEN_WIDTH;
//        frame.size.height = Animation_SCREEN_HEIGHT;
//        frame.origin.x = view.superview.center.x;
//        frame.origin.y = view.superview.center.y;
//        view.frame = frame;
////        CABasicAnimation *maskLayerAnimation = [CABasicAnimation animationWithKeyPath:@"bounds"];
////        maskLayerAnimation.fromValue         = (__bridge id)(startCycle.CGPath);
////        maskLayerAnimation.toValue           = (__bridge id)((endCycle.CGPath));
////        maskLayerAnimation.duration          = 60; //[self transitionDuration:transitionContext];
////        maskLayerAnimation.delegate          = self;
////        maskLayerAnimation.timingFunction =
////        [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
//        [maskLayer addAnimation:maskLayerAnimation forKey:@"bounds"];
    }else {
        [self loginErrorAnimation:view];
    }
}

- (void) loginErrorAnimation:(UIView *) view {
    [view.layer removeAllAnimations];
    [view.layer.mask removeFromSuperlayer];
    CALayer *viewLayer = view.layer;
    CGPoint position = viewLayer.position;
    CGPoint x = CGPointMake(position.x + 10, position.y);
    CGPoint y = CGPointMake(position.x - 10, position.y);
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]];
    [animation setFromValue:[NSValue valueWithCGPoint:x]];
    [animation setToValue:[NSValue valueWithCGPoint:y]];
    [animation setAutoreverses:YES];
    [animation setDuration:.06];
    [animation setRepeatCount:3];
    [viewLayer addAnimation:animation forKey:nil];
    AudioServicesPlaySystemSound(1521);
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    animation.fromValue         = @0;
    animation.toValue           = @(2 * M_PI);
    animation.repeatCount       = HUGE_VALF;
    animation.duration          = 1.0f;
    [self.view.layer addAnimation:animation forKey:@"rotate"];
}

+ (void)setRootVC:(UIViewController *)vc {
    //    [UIApplication sharedApplication].keyWindow.rootViewController = vc;

    [UIView transitionWithView:[UIApplication sharedApplication].keyWindow
                      duration:0.5f
                       options:UIViewAnimationOptionTransitionCrossDissolve
                    animations:^{
                        BOOL oldState = [UIView areAnimationsEnabled];
                        [UIView setAnimationsEnabled:NO];
                        [UIApplication sharedApplication].keyWindow.rootViewController = vc;
                        [UIView setAnimationsEnabled:oldState];
                    }
                    completion:^(BOOL finished){

                    }];
}

@end
